require('page/common/header/index.js');
require('./index.css');
var _util = require('util/util.js');

var _account_service = require('service/account-service.js')
var _cart_service = require('service/cart-service.js');

var loginForm = {
    status: true,
    init: function () {
        $('#loginMsg').hide();
        $('#signUp').hide();
        $('#signup').show();
        this.bindEvents();
        return this;
    },
    //点击事件
    bindEvents: function () {
        var _this = this;
     $('#submit').click  ( 
      function () {
        //采用密码登录
        if (_this.status) {
            var account = {
                username: $.trim($('#username').val()),
                password: $.trim($('#password').val())
            };
            console.log($('#username').val());
            _account_service.login(
                account,
                function (resp) {
                    console.log("登录成功yyyyyyy");
                    console.log(resp);
                    window.location.href = _util.getURLParam('redirect')||'./catalog-main.html';
                    _util.getURLParam('redirect');
                    console.log("_util.getURLParam('redirect');");

                    _cart_service.registerCart(
                        function (resp) {
                            console.log("registerCart");
                            console.log(resp);
                        },
                        function (errMsg) {
                            console.log("registerCart");
                            console.log(errMsg);
                        }
                    );
                    alert("登陆成功!欢迎" + resp.username);

                },
                function (errorMsg) {
                    $('#password').val("");
                    $('#passwordTips').text("");
                    $('#loginMsg').show().text(errorMsg);
                }
            )
        }
      }
    )
    $('#signup').click ( 
      function () {
        $('#signUp').show();
        $('#signup').hide();
      }
    )
    //注册
    $('#signUpSubmit').click (
        function () {
            if ($('#newPassword').val() === $('#repeatedPassword').val()) {
                _account_service.signUp(
                    $('#signUpForm').serialize(),
                    function (resp) {
                        alert("注册成功!欢迎" + resp.username);
                        window.location.href = './catalog-main.html';
                    },
                    function (errorMsg) {
                        alert("注册失败!,请检查您的信息" + errorMsg);
                    }
                )
            }
            else {
                alert("密码不一致");
            }
        })
        $('#newPassword').on('keyup', function () {

            if (this.value.length < 8 && this.value.length > 2) {
                $('#passwordTips').attr("class", 'successMsg').text('密码可用');
            }
            else {
                $('#passwordTips').attr("class", 'errorMsg').text('密码长度大于2不超过7');
            }
        });
        $('#repeatedPassword').on('keyup', function () {
            if (this.value === $('#newPassword').val()) {
                console.log("yes");
                $('#repeatedPasswordTips').attr("class", null).text('');
            }
            else {
                console.log("no");
                $('#repeatedPasswordTips').attr("class", 'errorMsg').text('密码不一致');
            }
        });
    },
};
loginForm.init();